<!DOCTYPE html>
<html lang="en">

<head>
    <title></title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="css/style.css" rel="stylesheet">
</head>

<body>
    <script>
        let m = 4
        let arr = [1, 2, 2, 3]
        let max = Math.max(...arr)
        //判断有没有相邻的
        let flag = false
        for (let i = 0; i < arr.length; i++) {
            if (arr[i] == max) {
                for (let j = i + 1; j < arr.length; j++) {
                    if (arr[j] == max) {
                        if ((j - i) % 2 == 0) {
                            i = j;
                        } else {
                            flag = true
                        }
                        break
                    }
                }
            }
        }

        let res
        let count = arr.reduce((a, b) => a + b)
        if (flag) {
            res = m * max - count + Math.floor(m / 2)
        } else {
            debugger
            if (m % 2 == 1 && arr.indexOf(max) % 2 == 0) {
                res = m * (max - 1) + Math.ceil(m / 2) - count
            } else {
                res = m * (max - 1) + Math.floor(m / 2) - count
            }
        }
        console.log(res)
    </script>
</body>

</html>